package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UserDept;
import com.ruoyi.system.service.IUserDeptService;
import com.ruoyi.web.core.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;

/**
 * 用户部门 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-29
 */
@Controller
@RequestMapping("/system/userDept")
public class UserDeptController extends BaseController
{
    private String prefix = "system/userDept";

	@Autowired
	private ISysUserService userService;
	
	@Autowired
	private IUserDeptService userDeptService;
	
	@RequiresPermissions("system:userDept:view")
	@GetMapping()
	public String userDept()
	{
	    return prefix + "/userDept";
	}
	
	/**
	 * 查询用户部门列表
	 */
	@RequiresPermissions("system:user:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysUser user)
	{
		startPage();
		List<SysUser> list = userService.selectUserList(user);
		return getDataTable(list);
	}
	
	/**
	 * 新增用户部门
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存用户部门
	 */
	@RequiresPermissions("system:userDept:add")
	@Log(title = "用户部门", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(UserDept userDept)
	{		
		return toAjax(userDeptService.insertUserDept(userDept));
	}

	/**
	 * 修改用户部门
	 */
	@GetMapping("/edit/{userId}")
	public String edit(@PathVariable("userId") Integer userId, ModelMap mmap)
	{
		UserDept userDept = userDeptService.selectUserDeptById(userId);
		mmap.put("userDept", userDept);
	    return prefix + "/edit";
	}

	/**
	 * 修改保存用户部门
	 */
	@RequiresPermissions("system:userDept:edit")
	@Log(title = "用户部门", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(UserDept userDept)
	{
		return toAjax(userDeptService.updateUserDept(userDept));
	}

	/**
	 * 删除用户部门
	 */
	@RequiresPermissions("system:userDept:remove")
	@Log(title = "用户部门", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(userDeptService.deleteUserDeptByIds(ids));
	}
	
}
